package com.ktjiaoyu.server.service.impl;


import com.ktjiaoyu.server.pojo.Admin;
import com.ktjiaoyu.server.service.IAdminService;
import com.ktjiaoyu.server.service.IRoleService;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    IAdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据提供用户名查询数据库有没有对应用户信息
        Admin admin = adminService.getAdminByUserName(username);
        if(admin==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //判断用户是否被禁用
        if(!admin.getState()){
            throw new DisabledException("用户已被禁用");
        }
        admin.setRoles(adminService.findRolesByAdminId(admin.getId()));
        return admin;
    }
}
